package com.chenfan.process.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenfan.process.common.utils.QueryWrapperUtils;
import com.chenfan.process.entity.po.ProcessDetailPo;
import com.chenfan.process.enums.ProcessResultTypeEnums;
import com.chenfan.process.repository.ProcessDetailRepository;
import com.chenfan.process.service.ProcessDetailService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * @author K
 * @date 2021/3/4
 */
@Service
public class ProcessDetailServiceImpl extends ServiceImpl<ProcessDetailRepository, ProcessDetailPo>
    implements ProcessDetailService {

    @Override
    public List<ProcessDetailPo> listApprovals(Collection<Long> approvalTemplateIdList) {
        QueryWrapper<ProcessDetailPo> queryWrapper = QueryWrapperUtils.newQueryWrapper();
        queryWrapper.in(!CollectionUtils.isEmpty(approvalTemplateIdList), ProcessDetailPo.APPROVAL_TEMPLATE_ID,
            approvalTemplateIdList);
        queryWrapper.notIn(ProcessDetailPo.RESULT_TYPE, Arrays.asList(ProcessResultTypeEnums.PASS.getIndex(),
            ProcessResultTypeEnums.REFUSED.getIndex(), ProcessResultTypeEnums.REVOKE.getIndex()));
        return list(queryWrapper);
    }

    @Override
    public List<ProcessDetailPo> listUnProcessApproval(Long approvalId) {
        QueryWrapper<ProcessDetailPo> queryWrapper = QueryWrapperUtils.newQueryWrapper();
        queryWrapper.eq(ProcessDetailPo.PARENT_ID, approvalId);
        queryWrapper.in(ProcessDetailPo.RESULT_TYPE, Arrays.asList(ProcessResultTypeEnums.CREATED.getIndex(),
            ProcessResultTypeEnums.PARENT.getIndex(), ProcessResultTypeEnums.END.getIndex()));
        return list(queryWrapper);
    }

    @Override
    public List<ProcessDetailPo> listProcessDetail(ProcessDetailPo processDetailPo) {
        QueryWrapper<ProcessDetailPo> queryWrapper = QueryWrapperUtils.newQueryWrapper();
        queryWrapper.eq(ProcessDetailPo.PARENT_ID, processDetailPo.getParentId());
        queryWrapper.eq(ProcessDetailPo.RESULT_TYPE, processDetailPo.getResultType());
        return list(queryWrapper);
    }

    @Override
    public List<ProcessDetailPo> listProcessDetailByApprovalId(Collection<Long> approvalIdList) {
        QueryWrapper<ProcessDetailPo> queryWrapper = QueryWrapperUtils.newQueryWrapper();
        queryWrapper.in(ProcessDetailPo.PARENT_ID, approvalIdList);
        return list(queryWrapper);
    }

    @Override
    public List<ProcessDetailPo> listApproving(List<Long> approvalIdList) {
        QueryWrapper<ProcessDetailPo> queryWrapper = QueryWrapperUtils.newQueryWrapper();
        queryWrapper.in(ProcessDetailPo.PARENT_ID, approvalIdList);
        queryWrapper.in(ProcessDetailPo.RESULT_TYPE,
            Arrays.asList(ProcessResultTypeEnums.CREATED.getIndex(), ProcessResultTypeEnums.PARENT.getIndex()));
        return list(queryWrapper);
    }

    @Override
    public List<ProcessDetailPo> listGroupInitData(Long approvalTemplateId, List<Long> beforeList) {
        QueryWrapper<ProcessDetailPo> queryWrapper = QueryWrapperUtils.newQueryWrapper();
        queryWrapper.eq(ProcessDetailPo.APPROVAL_TEMPLATE_ID, approvalTemplateId);
        queryWrapper.in(ProcessDetailPo.PROCESS_ID, beforeList);
        queryWrapper.in(ProcessDetailPo.RESULT_TYPE, Arrays.asList(ProcessResultTypeEnums.CREATED.getIndex(), ProcessResultTypeEnums.PARENT.getIndex()));
        return list(queryWrapper);
    }

    @Override
    public boolean saveOrUpdate(ProcessDetailPo entity) {
        return false;
    }
}
